﻿@model PollModel
@using Nop.Web.Framework;
@using Nop.Web.Models.Polls;
<div class="poll-item">
    <span class="poll-display-text">@Model.Name</span>
    @if (Model.AlreadyVoted)
    {
        <div class="poll-results">
            @foreach (var item in Model.Answers)
            {
                @string.Format(T("Polls.VotesResultLine").Text, item.Name, item.NumberOfVotes, item.PercentOfTotalVotes.ToString("0.0"))
                <br />
            }
            <span class="poll-total-votes">@string.Format(T("Polls.TotalVotes").Text, Model.TotalVotes)</span>
        </div>
    }
    else
    {
        <div id="pnl-poll-vote-@(Model.Id)" class="poll-take-poll">
            @foreach (var item in Model.Answers)
            {
                <input id="pollanswers-@(item.Id)" type="radio" name="pollanswers-@(Model.Id)" value="@item.Id" />
                <label for="pollanswers-@(item.Id)">@item.Name</label>
                <br />
            }
            <input type="submit" name="vote-poll-@(Model.Id)" value="@T("Polls.Vote")" id="vote-poll-@(Model.Id)" class="submitpollvotebutton" />
            <span id="poll-voting-progress-@(Model.Id)" style="display: none;">@T("Common.Wait...")
                <img src='@Url.Content("~/Content/Images/ajax_loader_small.gif")' alt="@T("Common.Wait...")" />
            </span>
        </div>
        <div class="clear">
        </div>
        <div id="pnl-poll-vote-result-@(Model.Id)">
        </div>
        
        <script type="text/javascript">
        $(document).ready(function () {
            $('#vote-poll-@(Model.Id)').click(function () {
                var pollAnswerId = $("input:radio[name=pollanswers-@(Model.Id)]:checked").val();
                if(typeof pollAnswerId == 'undefined'){
                    alert('@T("Polls.SelectAnswer")');
                }
                else {
                    var voteProgress = $("#poll-voting-progress-@(Model.Id)");
                    voteProgress.show();
                    $.ajax({
                        cache:false,
                        type: "POST",
                        url: "@(Url.Action("Vote", "Poll"))",
                        data: { "pollAnswerId": pollAnswerId },
                        success: function (data) {
                            voteProgress.hide();
                            $("#pnl-poll-vote-result-@(Model.Id)").html(data.Result);
                             if (data.Success) {
                                $('#pnl-poll-vote-@(Model.Id)').hide();                            
                             }
                             else {
                                $('#pnl-poll-vote-result-@(Model.Id)').fadeIn("slow").delay(2000).fadeOut("slow");
                             }
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            alert('Failed to vote.');
                            voteProgress.hide();
                        }  
                    });
                }
                return false;
            });
        });
        </script>
    }
</div>
